/**
 * @param {number} x
 * @return {number}
 */
var reverse = function (x) {
    if (x === 0) return 0;

    const max = 2 ** 31 - 1;
    const min = -1 * 2 ** 31;

    arrX = ('' + x).split('').reverse();

    while (arrX[0] == 0) {
        arrX.shift();
    }

    if (arrX[arrX.length - 1] == '-') {
        arrX.splice(arrX.length - 1, 1);
        arrX.splice(0, 0, '-');
    }

    const strX = arrX.join('');
    let newX = parseInt(strX);

    if (newX > max || newX < min) {
        console.log(0);
        return 0;
    }

    console.log(strX);
    return newX;
};

reverse(123);
reverse(-123);
reverse(1534236469);
